﻿using Abp.Domain.Services;
using Camc.Quality.Dto.ProcessesEntityDto;
using Camc.Quality.TechDoc;
using Camc.TechDoc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Camc.Quality.Quality
{
    public class TechDocClient : DomainService
    {
        //private readonly AppMes51CoreModuleConfig _configuration;
        //private readonly MESSoapClient _mesSoapClient;
        private readonly MESSoapClient _mesSoapClient;
        public TechDocClient(
        // AppMes51CoreModuleConfig configuration
        )
        {
            //    _configuration = configuration;
             _mesSoapClient = new MESSoapClient(MESSoapClient.EndpointConfiguration.MESSoap);
        }
        ///// <summary>
        ///// 根据图号获取pps列表
        ///// </summary>
        ///// <param name="drawingCode"></param>
        ///// <returns></returns>
        //public async Task<ICollection<Camc.Quality.TechDoc.TechDocDto>> FacekGetTechInfo(string drawingCode, string erpdeptid)
        //{

        //    return await FakeGetTechDocsByPps(drawingCode, "57");

        //}

        //private async Task<ICollection<Camc.Quality.TechDoc.TechDocDto>> FakeGetTechDocsByPps(string drawingCode, string workshopCode)
        //{
        //    var techDocList = new List<Camc.Quality.TechDoc.TechDocDto>();
        //    var dataA = new Camc.Quality.TechDoc.TechDocDto("1000", "GC1CE0-0S57026L1", "XX-1", "S", "drawingCodeA", "测试图1", "张三",
        //        new DateTime(2015, 10, 1), "file:///D:/workspace/GC1CE0-0S57026L1.txt", "51");
        //    var dataB = new TechDocDto("1111", "GC1CE0-0S57224Z1", "XX-2", "S", "drawingCodeA2", "测试图2", "李四",
        //        new DateTime(2015, 10, 2), "file:///D:/workspace/GC1CE0-0S57224Z1.txt", "51");
        //    techDocList.Add(dataA);
        //    techDocList.Add(dataB);
        //    return techDocList;
        //}
        /// <summary>
        /// 根据所选pps编号，获取pps文件内容
        /// </summary>
        /// <param name="codeId"></param>
        /// <returns></returns> 
        public async Task<Camc.Quality.TechDoc.TechDocDto> FakeGetTechDocByPps(string codeId, string workshopCode)
        {
            //switch (codeId)
            //{             

            //    case "Sml-10S112001Z1":
            //        var techDocA = new Camc.Quality.TechDoc.TechDocDto("1000", "Sml-10S112001Z1", "xinghao", "S", "drawingCodeA", "测试图1", "张三",
            //        new DateTime(2015, 10, 1), "file:///E:/0519.Html", "57");
            //        var procInfo1 = new ProcessesDto("49698550", "电搭接工作", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo2 = new ProcessesDto("49699102", "大部段搭接电阻测量", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo3 = new ProcessesDto("49720613", "仪器安装", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo4 = new ProcessesDto("49723992", "电缆敷设", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo5 = new ProcessesDto("49724087", "电连接器插接", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo6 = new ProcessesDto("49724105", "撤收", "", "内容1", "类型1", new Random().Next(1, 20));


            //        var procInfos = new List<ProcessesDto> { procInfo1, procInfo2, procInfo3 , procInfo4 , procInfo5 , procInfo6 /*, procInfo7, procInfo8, procInfo9, procInfo10, procInfo11, procInfo12, procInfo13,procInfo14 */};

            //        techDocA.Procs = procInfos;
            //        return techDocA;
            //    case "Sml-10S112001Z2":
            //        var techDocd = new Camc.Quality.TechDoc.TechDocDto("1000", "Sml-10S112001Z2", "xinghao", "S", "drawingCodeA", "测试图1", "张三",
            //        new DateTime(2015, 10, 1), "file:///E:/0528.Html", "57");
            //        var procInfo1d = new ProcessesDto("49698550", "电搭接工作", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo2d = new ProcessesDto("49699102", "大部段搭接电阻测量", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo3d = new ProcessesDto("49720613", "仪器安装", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo4d = new ProcessesDto("49723992", "电缆敷设", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo5d = new ProcessesDto("49724087", "电连接器插接", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo6d = new ProcessesDto("49724105", "撤收", "", "内容1", "类型1", new Random().Next(1, 20));


            //        var procInfosd = new List<ProcessesDto> { procInfo1d/*, procInfo2d, procInfo3d, procInfo4d, procInfo5d, procInfo6d , procInfo7, procInfo8, procInfo9, procInfo10, procInfo11, procInfo12, procInfo13,procInfo14 */};

            //        techDocd.Procs = procInfosd;
            //        return techDocd;

            //    case "GC测试文件1S112001Z2":
            //        var techDocB = new Camc.Quality.TechDoc.TechDocDto("1000", "GC测试文件1S112001Z2", "xinghao", "S", "drawingCodeB", "测试图1", "张三B",
            //        new DateTime(2015, 10, 1), "file:///F:/发次比对.Html", "57");
            //        var procInfo1B = new ProcessesDto("49837789", "检查", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo2B = new ProcessesDto("49837817", "安装", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo3B = new ProcessesDto("49837933", "防水防热", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo4B = new ProcessesDto("49837949", "记录", "", "内容1", "类型1", new Random().Next(1, 20));

            //        var procInfosB = new List<ProcessesDto> { procInfo1B, procInfo2B, procInfo3B, procInfo4B/*, procInfo5B, procInfo6B , procInfo7, procInfo8, procInfo9, procInfo10, procInfo11, procInfo12, procInfo13,procInfo14 */};
            //        techDocB.Procs = procInfosB;
            //        return techDocB;
            //    case "GCxx-10S11200002":
            //        var techDocC = new Camc.Quality.TechDoc.TechDocDto("1000", "GCxx-10S11200002", "xinghao", "S", "drawingCodeC", "测试图1", "张三C",
            //        new DateTime(2015, 10, 1), "file:///F:/05192.Html", "57");
            //        var procInfo1C = new ProcessesDto("49698550", "电搭接工作", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo2C = new ProcessesDto("49699102", "大部段搭接电阻测量", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo3C = new ProcessesDto("49720613", "仪器安装", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo4C = new ProcessesDto("49723992", "电缆敷设", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo5C = new ProcessesDto("49724087", "电连接器插接", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo6C = new ProcessesDto("49724105", "撤收", "", "内容1", "类型1", new Random().Next(1, 20));

            //        var procInfosC = new List<ProcessesDto> { procInfo1C, procInfo2C, procInfo3C, procInfo4C, procInfo5C, procInfo6C /*, procInfo7, procInfo8, procInfo9, procInfo10, procInfo11, procInfo12, procInfo13,procInfo14 */};
            //        techDocC.Procs = procInfosC;
            //        return techDocC;
            //    case "GCxx-10S112001Z1":
            //        var techDocF = new Camc.Quality.TechDoc.TechDocDto("1000", "GCxx-10S112001Z1", "xinghao", "S", "drawingCodeA", "测试图1", "张三",
            //        new DateTime(2015, 10, 1), "file:///E:/05193.Html", "57");
            //        var procInfo1F = new ProcessesDto("49698550", "电搭接工作", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo2F = new ProcessesDto("49699102", "大部段搭接电阻测量", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo3F = new ProcessesDto("49720613", "仪器安装", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo4F = new ProcessesDto("49723992", "电缆敷设", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo5F = new ProcessesDto("49724087", "电连接器插接", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo6F = new ProcessesDto("49724105", "撤收", "", "内容1", "类型1", new Random().Next(1, 20));


            //        var procInfosF = new List<ProcessesDto> { procInfo1F, procInfo2F, procInfo3F, procInfo4F, procInfo5F, procInfo6F /*, procInfo7, procInfo8, procInfo9, procInfo10, procInfo11, procInfo12, procInfo13,procInfo14 */};

            //        techDocF.Procs = procInfosF;
            //        return techDocF;
            //    case "GCxx-10S112001Z2":
            //        var techDocG = new Camc.Quality.TechDoc.TechDocDto("1000", "GCxx-10S112001Z2", "xinghao", "S", "drawingCodeA", "测试图1", "张三",
            //        new DateTime(2015, 10, 1), "file:///E:/05194.Html", "57");
            //        var procInfo1G = new ProcessesDto("49698550", "电搭接工作", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo2G = new ProcessesDto("49699102", "大部段搭接电阻测量", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo3G = new ProcessesDto("49720613", "仪器安装", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo4G = new ProcessesDto("49723992", "电缆敷设", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo5G = new ProcessesDto("49724087", "电连接器插接", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo6G = new ProcessesDto("49724105", "撤收", "", "内容1", "类型1", new Random().Next(1, 20));


            //        var procInfosG = new List<ProcessesDto> { procInfo1G, procInfo2G, procInfo3G, procInfo4G, procInfo5G, procInfo6G /*, procInfo7, procInfo8, procInfo9, procInfo10, procInfo11, procInfo12, procInfo13,procInfo14 */};

            //        techDocG.Procs = procInfosG;
            //        return techDocG;


            //    case "quan1":
            //        var techDocH = new Camc.Quality.TechDoc.TechDocDto("1000", "quan1", "xinghao", "S", "drawingCodeA", "测试图1", "张三",
            //        new DateTime(2015, 10, 1), "file:///E:/quan1.Html", "57");
            //        var procInfo1H = new ProcessesDto("49698550", "电搭接工作", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo2H = new ProcessesDto("49699102", "大部段搭接电阻测量", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo3H = new ProcessesDto("49720613", "仪器安装", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo4H = new ProcessesDto("49723992", "电缆敷设", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo5H = new ProcessesDto("49724087", "电连接器插接", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo6H = new ProcessesDto("49724105", "撤收", "", "内容1", "类型1", new Random().Next(1, 20));


            //        var procInfosH = new List<ProcessesDto> { procInfo1H, procInfo2H, procInfo3H, procInfo4H, procInfo5H, procInfo6H /*, procInfo7, procInfo8, procInfo9, procInfo10, procInfo11, procInfo12, procInfo13,procInfo14 */};

            //        techDocH.Procs = procInfosH;
            //        return techDocH;
            //    case "quan2":
            //        var techDocJ = new Camc.Quality.TechDoc.TechDocDto("1000", "quan2", "xinghao", "S", "drawingCodeA", "测试图1", "张三",
            //        new DateTime(2015, 10, 1), "file:///E:/quan2.Html", "57");
            //        var procInfo1J = new ProcessesDto("49698550", "电搭接工作", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo2J = new ProcessesDto("49699102", "大部段搭接电阻测量", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo3J = new ProcessesDto("49720613", "仪器安装", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo4J = new ProcessesDto("49723992", "电缆敷设", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo5J = new ProcessesDto("49724087", "电连接器插接", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo6J = new ProcessesDto("49724105", "撤收", "", "内容1", "类型1", new Random().Next(1, 20));


            //        var procInfosJ = new List<ProcessesDto> { procInfo1J, procInfo2J, procInfo3J, procInfo4J, procInfo5J, procInfo6J /*, procInfo7, procInfo8, procInfo9, procInfo10, procInfo11, procInfo12, procInfo13,procInfo14 */};

            //        techDocJ.Procs = procInfosJ;
            //        return techDocJ;
            //    case "shao":
            //        var techDocdI = new Camc.Quality.TechDoc.TechDocDto("1000", "shao", "xinghao111", "H", "drawin111", "测试图2", "张三2",
            //        new DateTime(2015, 10, 1), "file:///E:/shao.Html", "57");
            //        var procInfo1I = new ProcessesDto("49698550", "电搭接工作", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo2I = new ProcessesDto("49699102", "大部段搭接电阻测量", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo3I = new ProcessesDto("49720613", "仪器安装", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo4I = new ProcessesDto("49723992", "电缆敷设", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo5I = new ProcessesDto("49724087", "电连接器插接", "", "内容1", "类型1", new Random().Next(1, 20));
            //        var procInfo6I = new ProcessesDto("49724105", "撤收", "", "内容1", "类型1", new Random().Next(1, 20));


            //        var procInfosI = new List<ProcessesDto> { procInfo1I/*, procInfo2d, procInfo3d, procInfo4d, procInfo5d, procInfo6d , procInfo7, procInfo8, procInfo9, procInfo10, procInfo11, procInfo12, procInfo13,procInfo14 */};

            //        techDocdI.Procs = procInfosI;
            //        return techDocdI;
            //}

            return null;


        }


        public async Task<TechDocDto> RealGetTechDocsByPps(string codeId, string workshopCode)
        {
           // var docs = new List<TechDocDto>();
            // var result = await _mesSoapClient.GetTechInfoAsync(drawingCode, workshopCode);
            var tech = await _mesSoapClient.GetTechInfoByCodeIDAsync(codeId);
            //if (result != null)
            //{
            //    foreach (var tech in result)GetTechDocByPpsAsync
            //    {
            //        var doc = new TechDocDto(tech.PUID, tech.CodeID, tech.ProductID,
            //            tech.Phase, tech.DrawingID, tech.DrawingName,
            //            tech.Editor, Convert.ToDateTime(tech.EditTime), tech.URL, workshopCode);

            //        docs.Add(doc);
            //    }
            //}
            var doc = new TechDocDto(tech.PUID, tech.CodeID, tech.ProductID,
                        tech.Phase, tech.DrawingID, tech.DrawingName,
                        tech.Editor, Convert.ToDateTime(tech.EditTime), tech.URL, workshopCode);

            var procs = await _mesSoapClient.GetProcInfoListAsync(tech.PUID);
            var procInfos = new List<ProcessesDto>();
            if (procs != null)
            {

                foreach (var proc in procs)
                {


                    if (proc.Gongxuhao == "0" || proc.Status.ToString() == "删除") continue;

                    var p = new ProcessesDto(proc.ParUID, Convert.ToInt32(proc.Gongxuhao), proc.Mingcheng, proc.Gongzhong,
                        proc.Neirong, proc.Xingzhi, proc.QTime1, proc.URL);
                    procInfos.Add(p);
                }
            }
            doc.Procs = procInfos.OrderBy(p => p.Id).ToList();
            return doc;
        }

    }
}
